package com.lanou.bbs.service;

import com.lanou.bbs.dao.UserDao;
import com.lanou.bbs.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public User login(User user) throws SQLException {
        // 调用dao,通过username 查询数据库
        User u = userDao.findByUsername(user.getUsername());

        if (u != null && u.getPassword().equals(user.getPassword())) {
            // 登入成功
            return u;
        } else {
            // 登入失败
            return null;
        }
    }

    public boolean register(User user) {
        User u = null;

        try {
            u = userDao.findByUsername(user.getUsername());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (u != null) {
            return false;
        } else {
            try {
                userDao.add(user);
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }
}
